<?php

/**
 * Description of search
 * 
 * @author 叶风
 * Qq:    355007778
 * Email: ye.fn@163.com
 */

namespace c\goods;

class search extends g
{    
    function index_home()
    {
        $sA = array();
        $_POST['u'] = 'cgoods\search';
        $u_sort = \url::p('sid&p');
        $u_comp = \url::p('cid&p');
        
        // init
        $tables = array('t1'=>array('as'=>$this->table));
        $field = 't1.id, t1.title, t1.pic, t1.description, t1.hits, t1.url_title, t1.barcode, t1.money, t1.money_old, t1.num';
        $where = 't1.state>0';
        $db_se = $this->db->C('search');
        // order
        $orderA = $_GET['LO'];
        if ($orderA['col'])
         {
            $way = ($orderA['way']=='ASC' ? ' ASC' : ' DESC');
            $_A = array('hits', 'money', 'sales');
            $order = in_array($orderA['col'], $_A) ? $orderA['col'] . $way .',' :'';
         }
        else
            $order = '';
        
        // where start
        $keyword = $this->t_var['g_qw'] =  $this->P_G('qw');
        $sids = intval_ids($this->P_G('sid'));
        if ($sids){
            $s_ids = $this->S->ids_sub($sids);
            $tables['sm'] = array('as'=>$this->table_sortMap, 'join'=>'JOIN','on'=>'sm.data_id=t1.id');
            $db_se->group('t1.id');
            $where = 'sm.sort_id IN('. $s_ids .') AND ' . $where;
            
            $r = $this->S->li($sids);
            if (count($r)==1){
                $sA[] = array('n'=>$r[0]['name'], 'u'=>$u_sort);
                $sortNav = $this->S->pNav($r[0]['parent_id']);
             }
            else{
                $sidA = array();
                foreach (explode(',', $sids) as $v)
                        $sidA[$v] = $v;
                foreach ($r as $v)
                 {
                    $_ids = $sidA;
                    unset($_ids[$v['id']]);
                    $_ids = implode(',', $_ids);
                    $sA[] = array('n'=>$v['name'], 'u'=>$u_sort . $_ids);
                }
             }
         }
        elseif (!$keyword)
            \url::jump('?u=cgoods\sort\v');
         
        // keyword
        if ($keyword)
         {
            $kwR = \YF_f::initC('keyword')->splitKeyword($keyword, 3);
            if ($kwR)
             {
                $keyIDs = '';
                $keyA = array();
			 	foreach($kwR as $v)
			 	 {	
			 	 	$keyIDs .=','.$v['id'];
			 	 	$keyA[] = $v['word'];
			 	 }
                $keyIDs =  substr($keyIDs, 1);

        		$tables['kw'] = array('as'=>'keyword', 'join'=>'LEFT JOIN', 'on'=>"kw.id IN($keyIDs)");
                $db_se->group('t1.id');
                $w = 't1.title like CONCAT("%", kw.word, "%")';
                
                if ($keyA[0]!=$keyword && $order=='')
                 {
                     $w .= " or title like '%". $db_se->escape($keyword) ."%'";
                     $field .= ", IF(title like '%". $db_se->escape($keyword) ."%',999,FF) FF";
                 }
                $order .= 'FF DESC,TF DESC,';
             }
            else{
                $w = "title like '%". $db_se->escape($keyword) ."%'";
             }
            $where .= " AND ($w)";
         }
        
        // money
        $moneyA = $this->P_G('money');
        if ($moneyA)
         {
            $moneyA[0] = intval($moneyA[0]);
            $moneyA[1] = intval($moneyA[1]);
            $where .= ' AND t1.money >='. $moneyA[0];
            if ($moneyA[1] && $moneyA[1]>=$moneyA[0])
                $where .= ' AND t1.money <='. $moneyA[1];
            else
                $moneyA[1] = '';
         }

        // company
        if ($_GET['cid'])
         {
            $company_ids = intval_ids($_GET['cid']);
            $where .= " AND t1.company_id IN($company_ids)";
            
            $r = $this->db->get('company', 'id,name', "id IN($company_ids)");
            if (count($r)==1)
                $sA[] = array('n'=>$r[0]['name'], 'u'=>$u_comp);
            else{
                $_a = array();
                foreach (explode(',', $company_ids) as $v)
                        $_a[$v] = $v;
                foreach ($r as $v)
                 {
                    $_ids = $_a;
                    unset($_ids[$v['id']]);
                    $_ids = implode(',', $_ids);
                    $sA[] = array('n'=>$v['name'], 'u'=>$u_comp . $_ids);
                }
             }
         }
        $res = $db_se->get_page(array(), $tables, $field, $where, $order .'t1.id DESC');
        
        
        // sort option
        if (!isset($tables['sm']))
            $tables['sm'] = array('as'=>$this->table_sortMap, 'join'=>'JOIN','on'=>'sm.data_id=t1.id');
        $a = $this->db->getOne($tables, 'GROUP_CONCAT(DISTINCT(t1.company_id)) company_ids, GROUP_CONCAT(DISTINCT(sm.sort_id)) sort_ids', $where);
       
        if ($a['sort_ids'])
         {
            $w = "id IN({$a[sort_ids]}) AND state>0";
            if ($sids)
                $w .= " AND parent_id IN($sids)";
            else 
                $w .= " AND parent_id NOT IN({$a[sort_ids]})";
            $sortR = $this->db->get($this->S->table, 'id, name', $w, '', 20);
         }
         
        if ($a['company_ids'] && !$company_ids)
         {
            $w = "id IN({$a[company_ids]})";
            $companyR = $this->db->get('company', 'id,name', $w);
         }
        $this->db->paging = $db_se->paging;
		$this->loadTemplate('goods/list', array('res'=>$res, 'sA'=>$sA, 'moneyA'=>$moneyA,
                                            'sortNav'=>$sortNav, 'sortR'=>$sortR, 'u_sort'=> htmlq($u_sort),
                                            'companyR'=>$companyR, 'u_comp'=> htmlq($u_comp),
                                            'order_col'=>$orderA['col'], 'u_order'=> htmlq(\url::p('&LO&p').'&LO[col]='),
                                          ));
        $this->loadLayout('goods_search');
    }
}
